package com.erpanderp.wam.web;

import java.util.List;

import org.restlet.data.MediaType;
import org.restlet.data.Status;
import org.restlet.representation.Representation;
import org.restlet.representation.StringRepresentation;
import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;

import com.erpanderp.wam.usm.model.entity.user.Employee;
import com.erpanderp.wam.usm.model.service.user.EmployeeDAO;

public class WAMUsersResource extends ServerResource {

	@Get("xml")
	public Representation getXML() {
		String employeeName = (String) getRequest().getAttributes().get("en");
		List<Employee> employees = new EmployeeDAO().getEmployees(employeeName);
		if(employees == null) {
			setStatus(Status.CLIENT_ERROR_NOT_FOUND);
			return null;
		}
		
		StringBuffer sb = new StringBuffer("<employees>");
		
		for(Employee employee : employees){
			sb.append("<employee>");
				sb.append("<employeecode>");
				sb.append(employee.getEmployeeCode());
				sb.append("</employeecode>");
				sb.append("<firstname>");
				sb.append(employee.getFirstName());
				sb.append("</firstname>");
				sb.append("<lastname>");
				sb.append(employee.getLastName());
				sb.append("</lastname>");
				sb.append("<designation>");
				sb.append(employee.getDesignation());
				sb.append("</designation>");
				sb.append("<module>");
				sb.append(employee.getModuleName());
				sb.append("</module>");
				sb.append("<email>");
				sb.append(employee.getEmail());
				sb.append("</email>");
				sb.append("<mobile>");
				sb.append(employee.getTelephone());
				sb.append("</mobile>");
			sb.append("</employee>");
		}
		
		sb.append("</employees>");
		return new StringRepresentation(sb, MediaType.APPLICATION_XML);
	}

}